package com.sherry.servlet;

import com.alibaba.fastjson.JSONObject;
import com.sherry.service.IOrderService;
import com.sherry.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/order/delete")
public class DelOrderServlet extends HttpServlet {
    // 处理post请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("接收到前端POST请求");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String orderNo= request.getParameter("orderNo");
        IOrderService orderService=new OrderServiceImpl();
        Integer i= orderService.delOrderByNo(orderNo);

        System.out.println("orderService.delOrderByNo返回值= "+i);
        boolean flag=false;
        String msg="删除订单失败";
//        i>0代表有返回删除的水果的id，代表删除成功
        if(i>0){
            flag=true;
            msg="删除订单成功";
        }

        JSONObject data=new JSONObject();
        data.put("flag",flag);
        data.put("msg",msg);
        String dataStr=data.toJSONString();

        //响应
        PrintWriter write = response.getWriter();
        write.write(dataStr);
        write.flush();
    }

    // 处理get请求
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("接收到前端GET请求");
    }
}